<template>
  <div class="announcement-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>平台公告</span>
        </div>
      </template>

      <el-timeline v-loading="loading">
        <el-timeline-item 
          v-for="item in announcements" 
          :key="item.id"
          :timestamp="formatDateTime(item.publishTime)"
          placement="top"
        >
          <el-card>
            <div class="announcement-header">
              <div class="title">{{ item.title }}</div>
              <el-tag v-if="item.type === 1" type="danger" size="small">紧急通知</el-tag>
              <el-tag v-else-if="item.type === 2" type="warning" size="small">系统通知</el-tag>
            </div>
            <div class="content">{{ item.content }}</div>
          </el-card>
        </el-timeline-item>
      </el-timeline>

      <el-empty v-if="!loading && announcements.length === 0" description="暂无公告" />

      <div class="pagination" v-if="total > 0">
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="loadData"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getAnnouncementList } from '@/api/announcement'

const loading = ref(false)
const announcements = ref([])
const total = ref(0)
const current = ref(1)
const size = ref(10)

const formatDateTime = (dateTime) => {
  return new Date(dateTime).toLocaleString()
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getAnnouncementList({ current: current.value, size: size.value })
    announcements.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.announcement-container {
  max-width: 900px;
  margin: 0 auto;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.announcement-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.title {
  font-size: 16px;
  font-weight: bold;
  margin-right: 10px;
}

.content {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
  white-space: pre-wrap;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>

